﻿/*
 视频会议系统SDK
 */

using System.Linq;
using System;
using AxZWANPHONELib;
using System.Windows.Forms;
using TopVisionLog;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Win32;
using static TopVisionVideoStream.VideosManager;
using RestAPI;
using System.Net.Http;
using TopVisionVideoStream.Models;
using static TopVisionVideoStream.Configuration;
using System.Collections.Generic;

namespace TopVisionVideoStream
{
    /// <summary>
    /// SDK实例化对象
    /// </summary>
    public static class TVVS
    {

        /// <summary>
        /// 调试模式开关
        /// </summary>
        public static bool IsDebug = false;

        public static string Host => Helper.Host;

        public static bool NoCenter = false;

        /// <summary>
        /// 初始化标识
        /// </summary>
        static bool isInited { get { return Configuration.IsInited; } set { Configuration.IsInited = value; } }

        static string err = "";

        public static string IniZwanphone
        {
            get { return Configuration.iniZwanphone; }
            set { Configuration.iniZwanphone = value; }
        }


        #region Init

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="parent">控件容器</param>
        /// <returns></returns>
        public static bool Init(Control parent)
        {
            try
            {
                TVLog.WriteLine(">>TVVS SDK start Init >> Version:" + Helper.GetAssemblyInfoVersion());

                if (IsDebug)
                    TVLog.WriteLine(">>Debug Mode");

                if (parent == null)
                {
                    err = "Param:parent not null";

                    TVLog.WriteLine(err);

                    throw new Exception(err);
                }

                if (isInited)
                {
                    return false;
                }

                if (!IsDebug)
                {
                    if (!Helper.CheckCOM(Application.ExecutablePath))
                    {
                        TVLog.WriteLine("ERROR:Component path error:" + "\r\n----" + Helper.RegComPath + "\r\n----" + Helper.AppComPath);

                        return false;
                    }
                }

                var core = new AxZwanPhone();

                Configuration.Core = core;

                //core.CreateControl();

                ((System.ComponentModel.ISupportInitialize)(core)).BeginInit();

                //core.Refresh();

                parent.Controls.Add(core);

                core.Name = "ocxCore";

                core.Hide();

                ((System.ComponentModel.ISupportInitialize)(core)).EndInit();

                isInited = true;

                Helper.SetServerIP();

                CaptureManager.Init();

                if (NoCenter)
                {
                    AccountManager.Init();

                    VideoConferencManager.Init();
                }
                else
                {
                    core.TrafficNotify += Core_TrafficNotify;
                }

                _qosInformation = new Qos();



                return isInited;
            }
            catch (Exception ex)
            {
                TVLog.WriteLine(ex.Message);

                return false;
            }
        }


        #endregion


        #region Dispose

        /// <summary>
        /// 释放资源
        /// </summary>
        public static void Dispose()
        {
            if (isInited)
            {

                AccountManager.Dispose();

                VideoConferencManager.Dispose();

                Configuration.Dispose();

                CaptureManager.Dispose();

                VideosManager.Dispose();

                var core = Configuration.Core;

                if (core != null)
                    core.Dispose();
            }
        }

        #endregion


        #region Login /out

        public static string CompanyID => Configuration.CompanyID;

        /// <summary>
        /// 是否登录标识
        /// </summary>
        public static bool IsLoged { get { return AccountManager.IsLoged; } }
        public static event UnRegistEventHandler UnRegisterEvent
        {
            add { AccountManager.UnRegistEvent += value; }
            remove { AccountManager.UnRegistEvent -= value; }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="password">密码</param>
        /// <param name="result">登录回调，返回操作结果</param>
        /// <returns></returns>
        public static bool Login(string account, string password, LoginEvent result)
        {
            InitCheck();

            if (account == null | password == null)
            {
                err = "ERRPR:account | password could not be NULL";

                TVLog.WriteLine(err);

                throw new Exception(err);
            }

            if (result == null)
            {
                TVLog.WriteLine("LoginEvent do not setup");

                Console.WriteLine("LoginEvent do not setup");
            }

            return AccountManager.Login(account, password, result);
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="result">登出回调，返回操作结果</param>
        /// <returns></returns>
        public static bool Logout(LogoutEvent result)
        {
            InitCheck();

            if (result == null)
            {
                TVLog.WriteLine("LogoutEventdo not setup");

                Console.WriteLine("LogoutEventdo not setup");
            }

            return AccountManager.Logout(result);
        }

        #endregion


        #region Message Info

        public static event MessageInfoHandler MessageInfoEvent
        {
            add { VideoConferencManager.MessageInfoEvent += value; }
            remove { VideoConferencManager.MessageInfoEvent -= value; }
        }

        #endregion


        #region VideoConferenc

        /// <summary>
        /// 通话状态
        /// </summary>
        public static ConferencStatusType ConferencStatus { get { return VideoConferencManager.ConferencStatus; } }

        public static event VideoCallMeHandler VideoCallMe { add { VideoConferencManager.VideoCallMe += value; } remove { VideoConferencManager.VideoCallMe -= value; } }

        //public static event TrafficNotifyHandler TrafficNotifyEvent { add { Model_VideoConferenc.TrafficNotifyEvent += value; } remove { Model_VideoConferenc.TrafficNotifyEvent -= value; } }
        public static event TrafficNotifyHandler TrafficNotifyEvent;

        public static string CallindID { get { return Configuration.CallingID; } set { Configuration.CallingID = value; } }

        public static bool isMutilCalling { get { return Model_VideoConferenc.isMutilCalling; } set { Model_VideoConferenc.isMutilCalling = value; } }

        public static event VideoCallResultHandler VideoCallResult
        {
            add { VideoConferencManager.VideoCallResult += value; }
            remove { VideoConferencManager.VideoCallResult -= value; }
        }

        public static event VideoCallCancelResultHandler VideoCallCancelResult
        {
            add { VideoConferencManager.VideoCallCancelResult += value; }
            remove { VideoConferencManager.VideoCallCancelResult -= value; }
        }

        /// <summary>
        /// 视频呼叫
        /// </summary>
        /// <param name="callID">呼叫账号</param>
        /// <param name="callType">呼叫类型[CallType.Video:视频][CallType.Voice:语音]</param>
        /// <param name="result">呼叫操作回调</param>
        public static void VideoCall(string callID, CallType callType)
        {
            InitCheck();

            Configuration.CallingID = callID;

            VideoConferencManager.VideoCall(callID, callType);
        }


        /// <summary>
        /// 挂断
        /// </summary>
        /// <param name="result">挂断操作回调</param>
        public static bool VideoCallCancel()
        {
            InitCheck();

            return VideoConferencManager.VideoCallCancel();
        }

        public static bool AcceptCall()
        {
            return VideoConferencManager.AcceptCall();
        }

        /// <summary>
        /// 设置对方和本地画面句柄
        /// </summary>
        /// <param name="remoteHwnd">对方画面句柄</param>
        /// <param name="localHwnd">本地画面句柄</param>
        public static void SetScreen(int remoteHwnd, int localHwnd)
        {
            UIManager.SetScreen(remoteHwnd, localHwnd);
        }

        /// <summary>
        /// 对端类型回调事件
        /// </summary>
        public static event RemoteScreenTypeHandler RemoteScreenTypeEvent
        {
            add { Model_VideoConferenc.RemoteScreenTypeEvent += value; }
            remove { Model_VideoConferenc.RemoteScreenTypeEvent -= value; }
        }


        #region ConfCall


        public static string ConfID { get { return VideoConferencManager.ConfID; } }

        public static VideoConfMembers _VideoConfMembers { get { return Configuration._VideoConfMembers; } }

        public static bool ComConfEnable
        {
            get { return Configuration.CompanyMemberEnable & Configuration.ConfMemberEnable; }
            set { Configuration.CompanyMemberEnable = value; Configuration.ConfMemberEnable = value; }
        }

        /// <summary>
        /// 企业成员更新错误事件
        /// </summary>
        public static event ERRORCompanyMembersHandler ERRORCompanyMembers
        {
            add { Configuration.ERRORCompanyMembers += value; }
            remove { Configuration.ERRORCompanyMembers -= value; }
        }
        /// <summary>
        /// 会议成员更新错误事件
        /// </summary>
        public static event ERRORUpdateConfInfoHandler ERRORUpdateConfInfo
        {
            add { Configuration.ERRORUpdateConfInfo += value; }
            remove { Configuration.ERRORUpdateConfInfo -= value; }
        }
        /// <summary>
        /// 视频列表更新错误事件
        /// </summary>
        public static event ERRORVIdeoListHandler ERRORVIdeoList
        {
            add { VideosManager.ERRORVideoList += value; }
            remove { VideosManager.ERRORVideoList -= value; }
        }

        /// <summary>
        /// 发起会议
        /// </summary>
        /// <param name="members">会议成员</param>
        /// <param name="called">暂时不使用</param>
        /// <param name="callmode">呼叫模式 0_不需要录像 1_需要录像</param>
        /// <param name="screenmode">
        /// 视频会议屏幕模式，默认为1：
        /// 0_直播模式
        /// 1_两方视频通话（音量大的自动为主画面）
        /// 2_等大画面视频会议
        /// 3_一大多小画面视频会议（带大画面切换功能）
        /// </param>
        /// <param name="picnum">视频会议多画面时，最大显示方数（目前支持1，4，6），默认为1</param>
        /// <param name="result"></param>
        public static void VideoConfCall(string[] members, string called, string callmode, string screenmode, string picnum, string title, string rtmppush)
        {
            VideoConferencManager.VideoConfCall(members, called, callmode, screenmode, picnum, title, rtmppush);
        }
        /// <summary>
        /// 结束会议
        /// </summary>
        public static void VideoConfCtrlHalt()
        {
            VideoConferencManager.VideoConfCtrlHalt();
        }
        /// <summary>
        /// 会议中增加成员
        /// </summary>
        /// <param name="member"></param>
        public static void VideoConfCtlAdd(string member)
        {
            VideoConferencManager.VideoConfCtlAdd(member);
        }
        /// <summary>
        /// 会议中移除成员
        /// </summary>
        /// <param name="member"></param>
        public static void VideoConfCtlDel(string member)
        {
            VideoConferencManager.VideoConfCtlDel(member);
        }
        /// <summary>
        /// 会议中设置大画面
        /// </summary>
        /// <param name="member"></param>
        public static void VideoConfCtlView(string member)
        {
            VideoConferencManager.VideoConfCtlView(member);
        }
        /// <summary>
        /// 设置多人会议会议呼叫回调
        /// </summary>
        /// <param name="callback"></param>
        public static void SetVideoConfCallResult(VideoConfCallResultHandler callback)
        {
            Model_VideoConferenc.SetVideoConfCallResult(callback);
        }
        /// <summary>
        /// 设置多人会议挂断呼叫回调
        /// </summary>
        /// <param name="callback"></param>
        public static void SetVideoConfCallCtlHaltResult(VideoConfCallResultHandler callback)
        {
            Model_VideoConferenc.SetVideoConfCallCtlHaltResult(callback);
        }
        /// <summary>
        /// 设置多人会议邀请成员呼叫回调
        /// </summary>
        /// <param name="callback"></param>
        public static void SetVideoConfCallAddResult(VideoConfCallResultHandler callback)
        {
            Model_VideoConferenc.SetVideoConfCallAddResult(callback);
        }
        /// <summary>
        /// 设置多人会议移除成员回调
        /// </summary>
        /// <param name="callback"></param>
        public static void SetVideoConfCallDelResult(VideoConfCallResultHandler callback)
        {
            Model_VideoConferenc.SetVideoConfCallDelResult(callback);
        }
        /// <summary>
        /// 设置多人会议设置主画面回调
        /// </summary>
        /// <param name="callback"></param>
        public static void SetVideoConfCallViewResult(VideoConfCallResultHandler callback)
        {
            Model_VideoConferenc.SetVideoConfCallViewResult(callback);
        }
        /// <summary>
        /// 设置会议
        /// </summary>
        /// <param name="ccr"></param>
        public static async void SetConfCallResponse(ConfCallResponse ccr)
        {
            Model_VideoConferenc.SetConfCallResponse(ccr);
            await Task.Delay(100);
            Configuration.UpdateVideoConfMembers();
        }

        public static void SendConfMessage(string msg)
        {
            Model_VideoConferenc.SendConfMessage(msg);
        }
        #endregion


        #region Company Member 

        public static CompanyMembers _CompanyMembers { get { return Configuration._CompanyMembers; } }

        #endregion


        #region Features

        /// <summary>
        /// 闪光灯控制
        /// </summary>
        public static void Features_Flash()
        {
            VideoConferencManager.Features_Flash();
        }

        /// <summary>
        /// 对焦控制
        /// </summary>
        /// <param name="width">画面宽度</param>
        /// <param name="height">画面高度</param>
        /// <param name="x">鼠标X坐标</param>
        /// <param name="y">鼠标Y坐标</param>
        public static void Features_Focus(int width, int height, int x, int y)
        {
            VideoConferencManager.Features_Focus(width, height, x, y);
        }

        /// <summary>
        /// 变焦控制
        /// </summary>
        /// <param name="z">放大倍数[0-6]</param>
        public static void Features_Zoom(double z)
        {
            VideoConferencManager.Features_Zoom(z);
        }

        /// <summary>
        /// 标记控制
        /// </summary>
        /// <param name="width">画面宽度</param>
        /// <param name="height">画面高度</param>
        /// <param name="sX">标记起点X</param>
        /// <param name="sY">标记起点Y</param>
        /// <param name="eX">标记终点X</param>
        /// <param name="eY">标记终点Y</param>
        /// <param name="pT">标记类型
        /// 1 = 画直线    3 = 画空心矩形    5 = 空心圆    10 = 箭头
        /// </param>
        /// <param name="color">标记颜色
        /// #ff7200    #9943e3    #24def4    #07df8b    #288fe7    #ffe404     #ef3030     #000000
        /// 橙色       紫色       青色       绿色       蓝色       黄色        红色        黑色
        /// </param>
        /// <param name="pWidth">标记线宽
        /// 1,2,4,6
        /// </param>
        public static void Features_Mark(int width, int height, int sX, int sY, int eX, int eY, int pT, string color, int pWidth)
        {
            VideoConferencManager.Features_Mark(width, height, sX, sY, eX, eY, pT, color, pWidth);
        }

        /// <summary>
        /// 清除标记
        /// </summary>
        public static void Features_ClearMark()
        {
            VideoConferencManager.Features_ClearMark();
        }

        /// <summary>
        /// 拍照控制
        /// </summary>
        public static void Features_TakePhoto(TakephotoCompleteHandler TakephotoComplete)
        {
            VideoConferencManager.TakephotoComplete = TakephotoComplete;
            VideoConferencManager.Features_TakePhoto();
        }

        /// <summary>
        /// 退出拍照
        /// </summary>
        public static void Features_LeaveTakingPhoto()
        {
            VideoConferencManager.TakephotoComplete = null;
            VideoConferencManager.Features_LeaveTakingPhoto();
        }

        /// <summary>
        /// 发送文字
        /// </summary>
        /// <param name="txt"></param>
        public static void Features_SendText(string txt)
        {
            VideoConferencManager.Features_SendText(txt);
        }

        /// <summary>
        /// 发送文字 指定账号
        /// </summary>
        /// <param name="txt"></param>
        public static void Features_SendText(string id, string txt)
        {
            VideoConferencManager.Features_SendText(id, txt);
        }

        /// <summary>
        /// 清除文字
        /// </summary>
        public static void Features_ClearText()
        {
            VideoConferencManager.Features_ClearText();
        }

        /// <summary>
        /// 转语音控制
        /// </summary>
        public static void Features_VoiceOnly()
        {
            VideoConferencManager.Features_VoiceOnly();
        }

        /// <summary>
        /// 亮度调节
        /// </summary>
        /// <param name="value">亮度偏差值[正数增加，负数减少]</param>
        public static void Features_BrightnessModify(int value)
        {
            VideoConferencManager.Features_BrightnessModify(value);
        }

        /// <summary>
        /// 对比度调节
        /// </summary>
        /// <param name="value">对比度偏差值[正数增加，负数减少]</param> 
        public static void Features_ContrastModify(int value)
        {
            VideoConferencManager.Features_ContrastModify(value);
        }

        /// <summary>
        ///  设置分辨率
        /// </summary>
        /// <param name="_rsl">分辨率字串
        ///  "switch_cif";
        ///  "switch_vga";
        ///  "switch_720p";
        ///  "switch_1080p";
        /// </param>
        public static void Features_Resolution(string _rsl)
        {
            VideoConferencManager.Features_SetResolution(_rsl);
        }

        public static void Features_Bitrate(string bit)
        {
            VideoConferencManager.Features_SetBitrate(bit);
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msg"></param>
        public static void Features_SendMessage(string msg)
        {
            Model_VideoConferencFeatures.SendMessage(msg);
        }

        #endregion


        #region QosInfo
        private static Qos _qosInformation;

        public static Qos QosInformation
        {
            get
            {
                if (_qosInformation == null)
                    _qosInformation = new Qos();
                return _qosInformation;
            }
        }



        #endregion

        private static void Core_TrafficNotify(object sender, _DZwanPhoneEvents_TrafficNotifyEvent e)
        {
            TrafficNotifyEvent?.Invoke(e.audioSend, e.audioRecv, e.videoSend, e.videoRecv);
        }


        #endregion


        #region Video Look back

        /// <summary>
        /// 视频列表
        /// </summary>
        public static ConfVideo[] ConfVideos { get { return VideosManager.VideoList.ToArray(); } }
        public static string ServerUrl { get { return string.Format("http://{0}", RestAPICommon.ServerIP); } }
        public static string ServerAddress { get { return RestAPICommon.ServerIP; } set { RestAPICommon.ServerIP = value; } }
        public static bool VideoListEnable
        {
            get { return VideosManager.Enable; }
            set { VideosManager.Enable = value; }
        }
        public static bool VideoListUpdateAuto { get { return VideosManager.AutoUpdate; } set { VideosManager.AutoUpdate = value; } }
        //public static void GetVideoPath(VideoPathsHandler handler)
        //{
        //    VideosManager.GetVideoPath(handler);
        //}

        public static void GetVideoListAsync()
        {
            VideosManager.UpdateConfVideosHttpShoot();
        }

        #endregion


        #region Captures
        // new 

        public static bool IsLoadingCaptureList => CaptureManager.IsLoadingCaptureList;

        public static List<string> CameraNames => CaptureManager.CameraNames;
        public static List<string> MicrophoneNames => CaptureManager.MicrophoneNames;
        public static List<string> SpeakerNames => CaptureManager.SpeakerNames;

        public static string VideoProp => OcxApi.VideoProp;

        public static bool SetVideoDevice(int index)
        {
            return OcxApi.SetCamera(index);
        }
        public async static void SetCameraResolution(string param)
        {
            if (OcxApi.CamID == -2)
            {
                OcxApi.SetCameraResolution(param);

                return;
            }

            if (OcxApi.CamID == -1)
            {
                OcxApi.SetCameraResolution("240p");
                return;
            }

            //var rsls = await OcxApi.GetResolutions(OcxApi.CamID);

            //if (rsls == null || rsls.Length == 0)
            //{
            //    return;
            //}

            //bool ok = true;

            //switch (param.ToLower())
            //{
            //    case "240p":
            //        ok = rsls.Contains("320*240");
            //        break;
            //    case "480p":
            //        ok = rsls.Contains("640*480");
            //        break;
            //    case "720p":
            //        ok = rsls.Contains("1280*720");
            //        break;
            //    case "1080p":
            //        ok = rsls.Contains("1920*1080");
            //        break;
            //    default:
            //        break;
            //}

            //if (ok)
            OcxApi.SetCameraResolution(param);
        }

        public async static Task<string[]> GetResolution(int id)
        {
            return await OcxApi.GetResolutions(id);
        }

        public static void SetCameraBitrate(string bitrate, string rsl)
        {
            OcxApi.SetCameraBitrate(bitrate, rsl);
        }
        public static void SetCameraBitrate(uint bitrate)
        {
            //OcxApi.SetCameraBitrate(bitrate);
        }
        public static bool SetAudioDevice(int playback, int record)
        {
            return OcxApi.SetAudioDevices(playback, record);
        }

        public static bool SetMicrophoneParam(bool ec, bool ns, bool agc)
        {
            return OcxApi.SetMicrophoneParam(ec, ns, agc);
        }


        // --------------------------------------



        public static bool IsVideoLoading = false;

        public static void Screenshots(string filename)
        {
            OcxApi.Screenshots(filename);
        }

        public static int Volume => OcxApi.Volume;

        public static bool SetAudioVolume(int micState, int micLevel, int spkState, int spkVolume)
        {
            return OcxApi.SetAudioVolume(micState, micLevel, spkState, spkVolume);
        }


        ///// <summary>
        ///// 扬声器异常
        ///// </summary>
        //public static bool SpeakerErr { get { return CaptureManager.SpeakerErr; } }
        ///// <summary>
        ///// 麦克风异常
        ///// </summary>
        //public static bool MicrophoneErr { get { return CaptureManager.MicErr; } }
        //public static string[] Resolutions { get { return CaptureManager.Resolutions; } }

        //// default device
        //public static AudioDevice DefaultRecordDevice => TVMixer.Instance.DefaultRecordDevice;
        //public static AudioDevice DefaultPlaybackDevice => TVMixer.Instance.DefaultPlaybackDevice;

        //public static bool SetCaptures(int camIndex, int speakerIndex, int micIndex, Resolution rsl = Resolution.r480P)
        //{
        //    CaptureManager.SetVideoCapture(camIndex);
        //    if (!TVVS.SpeakerErr)
        //        CaptureManager.SetSpaker(speakerIndex);
        //    CaptureManager.SetMic(micIndex);
        //    CaptureManager.SetResolution(rsl);

        //    string log = string.Format("采集设备设置:摄像头:{0},扬声器:{1},麦克风:{2},分辨率:{3}", camIndex, speakerIndex, micIndex, rsl);
        //    Console.WriteLine(log);

        //    TVLog.WriteLine(log);

        //    return ApplyCaptures();
        //}

        //public static void SetCamera(int index)
        //{
        //    CaptureManager.SetVideoCapture(index);
        //}

        //public static void SetSpeaker(int index)
        //{
        //    CaptureManager.SetSpaker(index);
        //}

        //public static void SetMicrohpne(int index)
        //{
        //    CaptureManager.SetMic(index);
        //}

        //public static void SetCamResolution(Resolution rsl)
        //{
        //    CaptureManager.SetResolution(rsl);
        //}

        //public static void ApplyResolution()
        //{
        //    CaptureManager.SetVideoDeviceRsl();
        //}

        //public static void SetCaptures(int camIndex, int speakerIndex, int micIndex, string rsl = "AUTO")
        //{
        //    Resolution _rsl = Resolution.AUTO;
        //    switch (rsl)
        //    {
        //        case "AUTO":
        //            _rsl = Resolution.AUTO;
        //            break;
        //        case "240p":
        //            _rsl = Resolution.r240P;
        //            break;
        //        case "480p":
        //            _rsl = Resolution.r480P;
        //            break;
        //        case "720p":
        //            _rsl = Resolution.r720P;
        //            break;
        //        case "1080p":
        //            _rsl = Resolution.r1080P;
        //            break;
        //        case "自动":
        //            _rsl = Resolution.AUTO;
        //            break;
        //        case "流畅":
        //            _rsl = Resolution.r240P;
        //            break;
        //        case "标清":
        //            _rsl = Resolution.r480P;
        //            break;
        //        case "高清":
        //            _rsl = Resolution.r720P;
        //            break;
        //        case "超清":
        //            _rsl = Resolution.r1080P;
        //            break;
        //        default:
        //            break;
        //    }

        //    if (camIndex == -1)
        //        _rsl = Resolution.r240P;

        //    SetCaptures(camIndex, speakerIndex, micIndex, _rsl);
        //}

        //public static bool ApplyCaptures()
        //{
        //    return CaptureManager.SetCaptures();
        //}

        //// 设置摄像头
        //public static bool SetCamera(CameraDevice camera)
        //{
        //    return CaptureManager.SetCamera(camera);
        //}

        //public static void SetDisplayResolution(Resolution rsl)
        //{
        //    CaptureManager.SetDisplayResolution(rsl);
        //}

        //public static void SetDisplayResolution(string rsl)
        //{
        //    Resolution tmprsl = Resolution.r240P;

        //    switch (rsl)
        //    {
        //        case "AUTO":
        //            tmprsl = Resolution.AUTO;
        //            break;
        //        case "1080p":
        //            tmprsl = Resolution.r1080P;
        //            break;
        //        case "720p":
        //            tmprsl = Resolution.r720P;
        //            break;
        //        case "480p":
        //            tmprsl = Resolution.r480P;
        //            break;
        //        case "240p":
        //            tmprsl = Resolution.r240P;
        //            break;
        //        case "自动":
        //            tmprsl = Resolution.AUTO;
        //            break;
        //        case "超清":
        //            tmprsl = Resolution.r1080P;
        //            break;
        //        case "高清":
        //            tmprsl = Resolution.r720P;
        //            break;
        //        case "标清":
        //            tmprsl = Resolution.r480P;
        //            break;
        //        case "流畅":
        //            tmprsl = Resolution.r240P;
        //            break;
        //        case "switch_1080p":
        //            tmprsl = Resolution.r1080P;
        //            break;
        //        case "switch_720p":
        //            tmprsl = Resolution.r720P;
        //            break;
        //        case "switch_vga":
        //            tmprsl = Resolution.r480P;
        //            break;
        //        case "switch_cif":
        //            tmprsl = Resolution.r240P;
        //            break;
        //        default:
        //            break;
        //    }

        //    CaptureManager.SetDisplayResolution(tmprsl);
        //}

        //public static async void SetDisplay(CameraDevice camera, Resolution rsl)
        //{
        //    await SetDisplayAsync(camera, rsl);
        //}

        //public static async void SetDisplay(CameraDevice camera, string rsl)
        //{
        //    Resolution tmprsl = Resolution.r240P;

        //    switch (rsl)
        //    {
        //        case "AUTO":
        //            tmprsl = Resolution.AUTO;
        //            break;
        //        case "1080p":
        //            tmprsl = Resolution.r1080P;
        //            break;
        //        case "720p":
        //            tmprsl = Resolution.r720P;
        //            break;
        //        case "480p":
        //            tmprsl = Resolution.r480P;
        //            break;
        //        case "240p":
        //            tmprsl = Resolution.r240P;
        //            break;
        //        case "自动":
        //            tmprsl = Resolution.AUTO;
        //            break;
        //        case "超清":
        //            tmprsl = Resolution.r1080P;
        //            break;
        //        case "高清":
        //            tmprsl = Resolution.r720P;
        //            break;
        //        case "标准":
        //            tmprsl = Resolution.r480P;
        //            break;
        //        case "流畅":
        //            tmprsl = Resolution.r240P;
        //            break;
        //        default:
        //            break;
        //    }

        //    await SetDisplayAsync(camera, tmprsl);
        //}

        //public static async Task<bool> SetDisplayAsync(CameraDevice camera, Resolution rsl)
        //{
        //    try
        //    {
        //        bool ret = SetCamera(camera);
        //        if (camera == null)
        //            rsl = Resolution.r240P;
        //        if (ret)
        //        {
        //            await Task.Delay(3000);
        //            if (!Core.IsDisposed)
        //                SetDisplayResolution(rsl);
        //        }
        //        return ret;
        //    }
        //    catch (Exception ex)
        //    {
        //        string log = "SetDisplayAsync err :" + ex.Message;
        //        Console.WriteLine(log);
        //        TVLog.WriteLine(log);
        //        return false;
        //    }
        //}

        //// 屏幕共享

        //public static bool ScreenShare()
        //{
        //    return CaptureManager.ScreenShare();
        //}

        //public static async void ScreenShare(Resolution rsl)
        //{
        //    await ScreenShareAsync(rsl);
        //}

        //public static async void ScreenShare(string rsl)
        //{
        //    await ScreenShareAsync(rsl);
        //}

        //public static async Task<bool> ScreenShareAsync(Resolution rsl)
        //{
        //    bool ret = CaptureManager.ScreenShare();
        //    if (ret)
        //    {
        //        await Task.Delay(4000);
        //        SetDisplayResolution(rsl);
        //    }
        //    return ret;
        //}

        //public static async Task<bool> ScreenShareAsync(string rsl)
        //{
        //    bool ret = CaptureManager.ScreenShare();
        //    if (ret)
        //    {
        //        await Task.Delay(4000);
        //        SetDisplayResolution(rsl);
        //    }
        //    return ret;
        //}

        //// 设置麦克风、扬声器
        //public static bool SetAudioDevices(AudioDevice record, AudioDevice playback)
        //{
        //    return CaptureManager.SetAudioDevices(record, playback);
        //}

        //public static void UpdateCapturesList()
        //{
        //    CaptureManager.GetCaptures();
        //}

        //public static int SpeakerVolume
        //{
        //    get { return CaptureManager.SpeakerVolume; }
        //    set { CaptureManager.SpeakerVolume = value; }
        //}

        //public static bool SpeakerMute
        //{
        //    get { return CaptureManager.SpeakerMute; }
        //    set { CaptureManager.SpeakerMute = value; }
        //}

        //public static int MicrophoneLevel
        //{
        //    get { return CaptureManager.MicrophoneLevel; }
        //    set { CaptureManager.MicrophoneLevel = value; }
        //}

        //public static bool MicrophoneMute
        //{
        //    get { return CaptureManager.MicrophoneMute; }
        //    set { CaptureManager.MicrophoneMute = value; }
        //}

        //public static List<CameraDevice> WebCameras => CaptureManager.WebCameras;

        //public static void UpdateDeviceList()
        //{
        //    CaptureManager.UpdateDeviceList();
        //}

        //public static List<AudioDevice> PlaybackDevices => CaptureManager.PlaybackDevices;

        //public static List<AudioDevice> RecordDevices => CaptureManager.RecordDevices;

        //public static int CameraIndex => CaptureManager.CurCamIndex;

        //public static int SpeakerIndex => CaptureManager.CurSpeakerIndex;

        //public static int MicrophoneIndex => CaptureManager.CurMicIndex;

        //public static Resolution CameraResolution
        //{
        //    get
        //    {
        //        Resolution rsl = Resolution.AUTO;

        //        switch (CaptureManager.CurResolution)
        //        {
        //            case "AUTO":
        //                rsl = Resolution.AUTO;
        //                break;
        //            case "1080p":
        //                rsl = Resolution.r1080P;
        //                break;
        //            case "720p":
        //                rsl = Resolution.r720P;
        //                break;
        //            case "480p":
        //                rsl = Resolution.r480P;
        //                break;
        //            case "240p":
        //                rsl = Resolution.r240P;
        //                break;
        //        }
        //        return rsl;
        //    }
        //}


        ///// <summary>
        ///// 麦克风属性设置
        ///// </summary>
        ///// <param name="ec">回声抑制</param>
        ///// <param name="ns">噪音消除</param>
        ///// <param name="agc">自动增益</param>
        //public static void SetMicrophoneProperty(bool ec, bool ns, bool agc)
        //{
        //    CaptureManager.SetMicProperty(ec, ns, agc);
        //}

        //// TVMixer
        //public static TVMixer TVMixer => TVMixer.Instance;

        //public static void SetVideoBitrate(string param)
        //{
        //    int bitrate = 512;
        //    switch (param)
        //    {
        //        case "超清":
        //            bitrate = 4096;
        //            break;
        //        case "高清":
        //            bitrate = 2048;
        //            break;
        //        case "标清":
        //            bitrate = 1024;
        //            break;
        //        case "流畅":
        //            bitrate = 512;
        //            break;
        //        default:
        //            break;
        //    }

        //    OcxApi.SetCameraBitrate((bitrate * 1000).ToString()); ;
        //}

        #endregion


        #region Helper

        /// <summary>
        /// 初始化检测
        /// </summary>
        private static void InitCheck()
        {
            if (!isInited)
            {
                err = "ERROR:core is not inited";

                TVLog.WriteLine(err);

                throw new Exception(err);
            }
        }

        /// <summary>
        /// 上传接口地址
        /// </summary>
        public static string GetUploadAddress { get { return Configuration.topvision_api + Configuration.UploadAddress; } }

        #endregion


        #region Server Config

        public static ServerType _ServerType => Configuration._ServerType;
        //public static string IniZwanphone => Configuration.iniZwanphone;
        public static string BASE_CFG => Helper.SOFTDA_CFG;
        public static string SERVER => Helper.SERVER;
        public static string FEC => "FEC";

        public static void UpdateServerIP()
        {
            Helper.SetServerIP();
        }

        public static void SetServerIP(string ipAddress, int port = 8080)
        {
            RestAPICommon.ServerIP = ipAddress;
            RestAPICommon.ServerPort = ":" + port;
        }

        public static void SetServerPort(int port)
        {
            RestAPICommon.ServerPort = ":" + port;
        }

        public static void SetServerTag(ServerType server)
        {
            INIOperation.INIWriteValue(TVVS.IniZwanphone, TVVS.BASE_CFG, TVVS.SERVER, ((int)server).ToString());
        }

        public static void SetServerTag(string tag)
        {
            INIOperation.INIWriteValue(TVVS.IniZwanphone, TVVS.BASE_CFG, TVVS.SERVER, tag);
        }

        public static void SetFEC(bool value)
        {
            INIOperation.INIWriteValue(TVVS.IniZwanphone, TVVS.BASE_CFG, FEC, value ? "1" : "0");
        }

        public static string GetFEC()
        {
            return INIOperation.INIGetStringValue(TVVS.IniZwanphone, TVVS.BASE_CFG, FEC, "1");
        }

        #endregion



        #region 管理


        ///// <summary>
        ///// Test
        ///// </summary>
        ///// <param name="callID"></param>
        ///// <returns></returns>
        //public static async Task<CallStatusType> GetAccountStatus(string callID, string cpID, string key)
        //{
        //    Configuration.CompanyID = cpID;

        //    Configuration.SetDisKey(key);

        //    return await Configuration.GetAccountStatusAsync(callID);
        //}

        //public static void GetAgentstatus(GetPostResponseHandler result)
        //{
        //    Configuration.GetAgentstatus(result);
        //}

        //private static bool pwdResult = false;
        //private static string pwdStr = "";
        //public async static Task<string> ChangePWD(string account, string oldPassword, string newPassword)
        //{
        //    if (string.IsNullOrWhiteSpace(account) | string.IsNullOrWhiteSpace(oldPassword) | string.IsNullOrWhiteSpace(newPassword))
        //    {
        //        return "";
        //    }

        //    MChangepwd mcPWD = new MChangepwd(account, oldPassword, newPassword);
        //    mcPWD.Call(ChangePwdResult);
        //    pwdResult = false;

        //    while (!pwdResult)
        //    {
        //        await Task.Delay(13);
        //    }

        //    return pwdStr;
        //}

        //private static void ChangePwdResult(string response)
        //{
        //    pwdStr = response;
        //    pwdResult = true;
        //    Console.WriteLine("修改SIP账户密码 ： " + response);
        //}

        #endregion


        #region 重庆大学

        public static void SetServerIP(string ip)
        {
            RestAPICommon.ServerIP = ip;
        }

        public static void SetServerPort(string port)
        {
            RestAPICommon.ServerPort = port;
        }

        public static void SetRecordUrlPath(string path)
        {
            VideosManager.recordPathHeader = path;
        }

        public static void SetTopVision_API(string path)
        {
            Configuration.topvision_api = path;
        }

        #endregion

    }
}

